{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "85f88996-cf08-4a53-b102-247e38229134",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-12-11T01:11:43.698003Z",
     "iopub.status.busy": "2024-12-11T01:11:43.697493Z",
     "iopub.status.idle": "2024-12-11T01:11:47.439010Z",
     "msg_id": "a1453be6-d07f-4986-a137-d9b4b183e5e0",
     "shell.execute_reply": "2024-12-11T01:11:47.437983Z",
     "shell.execute_reply.started": "2024-12-11T01:11:43.697972Z"
    }
   },
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'seaborn'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 27\u001b[0m\n\u001b[1;32m     24\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m rcParams\n\u001b[1;32m     25\u001b[0m rcParams[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfont.family\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSimHei\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m---> 27\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mseaborn\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01msns\u001b[39;00m\n\u001b[1;32m     29\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mitertools\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m combinations \n\u001b[1;32m     30\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpickle\u001b[39;00m\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'seaborn'"
     ]
    }
   ],
   "source": [
    "import pandas as pd \n",
    "import numpy as np \n",
    "import os \n",
    "import gc\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import roc_curve\n",
    "from sklearn.model_selection import KFold, StratifiedKFold\n",
    "from sklearn.feature_selection import RFECV\n",
    "\n",
    "# import lightgbm as lgb \n",
    "# from lightgbm import log_evaluation, early_stopping\n",
    "import xgboost as xgb \n",
    "import copy \n",
    "\n",
    "from sklearn.ensemble import IsolationForest\n",
    "\n",
    "import matplotlib.pyplot as plt \n",
    "from matplotlib import rcParams\n",
    "rcParams[\"font.family\"] = \"SimHei\"\n",
    "\n",
    "import seaborn as sns\n",
    "\n",
    "from itertools import combinations \n",
    "import pickle\n",
    "# from bayes_opt import BayesianOptimization\n",
    "# import optuna\n",
    "from functools import partial\n",
    "\n",
    "import networkx as nx \n",
    "from itertools import combinations\n",
    "from functools import partial\n",
    "from sklearn.metrics import roc_auc_score\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "from sklearn.preprocessing import MinMaxScaler,StandardScaler\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.linear_model import LogisticRegressionCV\n",
    "from sklearn.metrics import confusion_matrix,accuracy_score,classification_report,roc_auc_score,log_loss\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "import sklearn.metrics as metrics\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.metrics import roc_curve\n",
    "from sklearn.model_selection import KFold\n",
    "from sklearn.feature_selection import RFECV\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "import sklearn.ensemble as ensemble\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn import svm\n",
    "from sklearn.feature_selection import SelectFromModel\n",
    "\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from sklearn.metrics import f1_score\n",
    "\n",
    "import xgboost as xgb\n",
    "from xgboost import XGBClassifier\n",
    "\n",
    "import lightgbm as lgb\n",
    "from lightgbm import LGBMClassifier\n",
    "from lightgbm import log_evaluation, early_stopping\n",
    "\n",
    "import catboost as cbt\n",
    "from catboost import CatBoostClassifier\n",
    "\n",
    "from scipy import stats,integrate\n",
    "from scipy.stats import ks_2samp\n",
    "#from scipy.stats import kssamp\n",
    "from scipy.stats import pearsonr\n",
    "from sklearn.model_selection import RandomizedSearchCV\n",
    "from scipy.stats import uniform\n",
    "from scipy.stats import kstest\n",
    "\n",
    "import toad\n",
    "\n",
    "pd.set_option('display.max_columns', 50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a94c8ea2-9a70-4208-8448-e7d5640164c3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:13:12.966179Z",
     "iopub.status.busy": "2024-11-12T01:13:12.965445Z",
     "iopub.status.idle": "2024-11-12T01:13:12.969527Z",
     "msg_id": "1d219185-8c47-4103-850a-e1543451eb3e",
     "shell.execute_reply": "2024-11-12T01:13:12.968884Z",
     "shell.execute_reply.started": "2024-11-12T01:13:12.966148Z"
    }
   },
   "outputs": [],
   "source": [
    "import catboost as cb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aec48db7-13b0-42ab-bc57-4b98a0db7741",
   "metadata": {},
   "source": [
    "# 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e3b8f266-593c-413e-8267-9960e7d5eb96",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:13:15.960506Z",
     "iopub.status.busy": "2024-11-12T01:13:15.959993Z",
     "iopub.status.idle": "2024-11-12T01:13:15.971293Z",
     "msg_id": "18495b26-2304-421a-b0d5-1a7ae9223500",
     "shell.execute_reply": "2024-11-12T01:13:15.970514Z",
     "shell.execute_reply.started": "2024-11-12T01:13:15.960475Z"
    }
   },
   "outputs": [],
   "source": [
    "def get_data(file_name, num_rows=None):\n",
    "    train_path = \"/home/mole/work/contest/train\"\n",
    "    test_path = \"/home/mole/work/contest/B\"\n",
    "    df_train = pd.read_csv(os.path.join(train_path, file_name + \"_T.csv\"), nrows=num_rows)\n",
    "    df_test = pd.read_csv(os.path.join(test_path, file_name + \"_B.csv\"), nrows=num_rows)\n",
    "    df_train[\"is_train\"] = 1\n",
    "    df_test[\"is_train\"] = 0\n",
    "    \n",
    "    df = pd.concat(objs=[df_train, df_test],axis=0)\n",
    "    df.rename(mapper = {'DATA_DAT': '数据日期', 'CUST_NO': '客户编号', 'OPTO': '经营期限至', 'OPFROM': '经营期限自', 'ENTSTATUS': '经营状态', 'REGCAP': '注册资本', 'ESDATE': '成立日期', 'FRNAME': '法定代表人/负责人/执行事务合伙人', 'ENTTYPE_CD': '企业（机构）类型编码', 'REGPROVIN_CD': '所在省份编码', 'INDS_CD': '国民经济行业代码', 'ALTDATE': '变更日期', 'ALTITEM': '变更事项', 'PERNAME': '人员姓名', 'POSITIONCODE': '职位代码', 'PERSONAMOUNT': '人员总数量', 'WEBTYPE': '网站（网店）类型', 'WEBSITNAME': '网站（网店）名称', 'DOMAIN': '网站（网店）地址', 'ANCHEDATE': '年报日期', 'ANCHEYEAR': '年报年份', 'EXECMONEY': '执行标的', 'REGDATECLEAN': '立案时间', 'COURTNAME': '执行法院', 'CASECODE': '案号', 'PUBLISHDATECLEAN': '发布时间', 'GISTID': '执行依据文号', 'PERFORMANCE': '被执行人履行情况', 'REGDATE': '立案时间', 'FINALDATE': '终本日期', 'UNPERFMONEY': '未履行金额', 'CONDATE': '出资日期', 'SUBCONAM': '认缴出资额（万元）', 'FUNDEDRATIO': '出资比例', 'INVTYPE': '股东类型', 'CONFORM': '出资方式', 'SH_CUST_NO': '股东客户编号', 'BTD_BEGINDATE': '所属日期起', 'BTD_ENDDATE': '所属日期止', 'BTD_COLLECTCODE': '征收项目代码', 'BTD_DECLARDATE': '申报日期', 'BTD_DECLARTERM': '申报期限', 'BTD_TOTALSALE': '全部销售收入', 'BTD_TAXABLESALE': '应税销售收入', 'BTD_TAXPAYABLE': '应纳税额', 'BTD_DEDUCTAMOUNT': '减免税额', 'TR_DAT': '交易日期', 'TR_CD': '交易代码', 'CHANL_CD': '渠道代码', 'ABS_INFO': '摘要信息', 'CPT_TYP_CD': '交易对手类型代码', 'ARG_ACCT_BAL': '合约账户余额', 'ACTG_DIRET_CD': '记账方向代码', 'TRS_CSH_IND': '现转标识', 'CSH_EX_IND': '钞汇标识', 'RMB_TR_AMT': '折人民币交易金额', 'CPT_INTL_FE_CUST_IND': '对手方行内客户标识', 'INT_BNK_TR_IND': '是否跨行交易', 'SAME_NAM_IND': '同名账户标识', 'CPT_CUST_NO': '交易对手客户编号'},\n",
    "              axis=1,\n",
    "              inplace=True\n",
    "             )\n",
    "    return df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb2e163e-f4fc-45f3-a2c4-0d2aede47a94",
   "metadata": {},
   "source": [
    "# 模型训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0335dac9-74e9-448a-ad0d-47c40a7ae232",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:13:25.173749Z",
     "iopub.status.busy": "2024-11-12T01:13:25.173198Z",
     "iopub.status.idle": "2024-11-12T01:13:25.184510Z",
     "msg_id": "d933ea96-93fe-4351-b209-eda262641008",
     "shell.execute_reply": "2024-11-12T01:13:25.183786Z",
     "shell.execute_reply.started": "2024-11-12T01:13:25.173720Z"
    }
   },
   "outputs": [],
   "source": [
    "def XGB_model(\n",
    "              X=None,\n",
    "              y=None,\n",
    "              params=None,\n",
    "              num_boost_round=10000,\n",
    "              early_stopping_rounds=200,\n",
    "              cv=StratifiedKFold(n_splits=5, shuffle=True, random_state=2022)\n",
    "             ):\n",
    "    \n",
    "    \n",
    "    if params is None:\n",
    "        params = {\n",
    "                'booster': 'gbtree',\n",
    "                'eval_metric': 'auc',\n",
    "                'gamma': 1,\n",
    "                'min_child_weight': 50,\n",
    "                'max_depth': 3,\n",
    "                'lambda':1,\n",
    "                'objective':'binary:logistic',\n",
    "                'learning_rate': 0.01,\n",
    "                'random_state':2022\n",
    "                }\n",
    "\n",
    "    columns = X.columns.tolist() \n",
    "    \n",
    "    y_oof = np.zeros(X.shape[0])\n",
    "    score = 0\n",
    "    clfs = []\n",
    "    ks_list = []\n",
    "    for k, (trian_index, valid_index) in enumerate(cv.split(X, y)):\n",
    "        X_train, y_train = X.values[trian_index], y.values[trian_index]\n",
    "        X_valid, y_valid = X.values[valid_index], y.values[valid_index]\n",
    "        train_matrix = xgb.DMatrix(data=X_train, label=y_train)\n",
    "        valid_matrix = xgb.DMatrix(data=X_valid, label=y_valid)\n",
    "        \n",
    "        watch_list = [(train_matrix, 'train'), (valid_matrix, 'valid')]\n",
    "        clf = xgb.train(params, \n",
    "                          train_matrix, \n",
    "                          evals=watch_list, \n",
    "                          num_boost_round=num_boost_round, \n",
    "                          early_stopping_rounds=early_stopping_rounds,\n",
    "                          verbose_eval=100, \n",
    "                         )\n",
    "        y_pred_valid = clf.predict(valid_matrix)\n",
    "        \n",
    "        y_oof[valid_index] = y_pred_valid\n",
    "        print(\"=======================================\")\n",
    "        print(\"第 {} 折，当前 KS = {:.6}\".format(k+1, get_KS(y_valid, y_pred_valid)))\n",
    "        print(\"=======================================\")\n",
    "        score = score + get_KS(y_valid, y_pred_valid)\n",
    "        ks_list.append(get_KS(y_valid, y_pred_valid))\n",
    "        \n",
    "        del X_train, X_valid, y_train, y_valid\n",
    "        gc.collect()\n",
    "        \n",
    "        clfs.append(clf)\n",
    "    \n",
    "    print(\"平均 KS = {:.6}\".format(score/(k+1)))\n",
    "    print(\"Out of folds KS = {:.6}\".format(get_KS(y, y_oof)))\n",
    "    \n",
    "    return clfs, ks_list, get_KS(y, y_oof)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b765a82-eb46-424c-9c43-bec755069d11",
   "metadata": {},
   "source": [
    "# 标签信息表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a3adb0a3-62d0-4672-9adc-fb30c177408b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:13:32.050207Z",
     "iopub.status.busy": "2024-11-12T01:13:32.049686Z",
     "iopub.status.idle": "2024-11-12T01:13:32.121009Z",
     "msg_id": "9ce7deec-58fa-4a63-95fa-6f8132b2d6b4",
     "shell.execute_reply": "2024-11-12T01:13:32.120290Z",
     "shell.execute_reply.started": "2024-11-12T01:13:32.050175Z"
    }
   },
   "outputs": [],
   "source": [
    "TARGET = get_data(\"XW_ENTINFO_TARGET\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25b7f9c3-3e66-41f2-a206-9a693328dc6f",
   "metadata": {},
   "source": [
    "# 拼接数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2840886e-d3e6-4d8f-830e-1960cada3bef",
   "metadata": {},
   "source": [
    "## 企业基本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2df11e2d-cf3b-4c16-8365-742fa029e4d8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:15:22.101067Z",
     "iopub.status.busy": "2024-11-12T01:15:22.100540Z",
     "iopub.status.idle": "2024-11-12T01:15:22.255345Z",
     "msg_id": "524643eb-df3d-4f2e-845c-e995e733b7a1",
     "shell.execute_reply": "2024-11-12T01:15:22.254580Z",
     "shell.execute_reply.started": "2024-11-12T01:15:22.101034Z"
    }
   },
   "outputs": [],
   "source": [
    "df_tyy = pd.read_pickle(\"../data_bak/TYY_企业基本信息B榜.pkl\").drop(columns=['经营期限至', '经营期限自', '成立日期', ])\n",
    "\n",
    "df_hyy = pd.read_pickle(\"../data_bak/基本信息表_原本数据加文本特征_B榜.pkl\")\n",
    "\n",
    "df_xkz = pd.read_pickle(\"../data_bak/数据还原_企业基本信息表特征_细化_类别转码特征_剔除标签编码.pkl\").drop(columns=['数据日期', '经营期限至', '经营期限自', '经营状态', '注册资本', '成立日期', '法定代表人/负责人/执行事务合伙人', '企业（机构）类型编码', '所在省份编码', '国民经济行业代码', 'is_train'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fe69d4cc-8285-485d-a60d-05a348090759",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:15:23.396319Z",
     "iopub.status.busy": "2024-11-12T01:15:23.395789Z",
     "iopub.status.idle": "2024-11-12T01:15:23.808903Z",
     "msg_id": "3b4f4d0e-5122-467e-af1c-f1f94717275d",
     "shell.execute_reply": "2024-11-12T01:15:23.808106Z",
     "shell.execute_reply.started": "2024-11-12T01:15:23.396286Z"
    }
   },
   "outputs": [],
   "source": [
    "df_企业基本信息 = df_hyy.merge(df_xkz, how=\"left\", on=\"客户编号\")\n",
    "df_企业基本信息 = df_企业基本信息.merge(df_tyy, how=\"left\", on=\"客户编号\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "76498d70-483f-4263-af47-a2714d9d322b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:15:24.877420Z",
     "iopub.status.busy": "2024-11-12T01:15:24.876892Z",
     "iopub.status.idle": "2024-11-12T01:15:24.883622Z",
     "msg_id": "902771b7-78ad-4b3f-8021-25deaab38b1e",
     "shell.execute_reply": "2024-11-12T01:15:24.882957Z",
     "shell.execute_reply.started": "2024-11-12T01:15:24.877390Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(59116, 167)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_企业基本信息.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6d84595-aec8-4721-ac24-a12fe0d1c21c",
   "metadata": {},
   "source": [
    "## 金融交易流水"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "0565962a-43f3-4790-b9ac-e8a23d01a51e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:16:45.175953Z",
     "iopub.status.busy": "2024-11-12T01:16:45.175424Z",
     "iopub.status.idle": "2024-11-12T01:16:46.078269Z",
     "msg_id": "2ab28a61-29df-4156-823d-569f4a9a1074",
     "shell.execute_reply": "2024-11-12T01:16:46.077480Z",
     "shell.execute_reply.started": "2024-11-12T01:16:45.175921Z"
    }
   },
   "outputs": [],
   "source": [
    "df_tyy = pd.read_pickle(\"../data_bak/TYY_企业金融性交易明B榜.pkl\")\n",
    "\n",
    "df_xkz = pd.read_pickle(\"../data_bak/B_金融性交易明细_新版本全部特征相关性剔除后保留543.pkl\")\n",
    "\n",
    "df_hyy = pd.read_pickle(\"../data_bak/交易流水表_原本数据加文本特征_B榜.pkl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b1eee32d-6d30-49c2-b5a1-9df13f9e52e5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:16:47.257440Z",
     "iopub.status.busy": "2024-11-12T01:16:47.256884Z",
     "iopub.status.idle": "2024-11-12T01:16:51.638110Z",
     "msg_id": "228582e4-847d-4c8e-a60f-f2210288ef43",
     "shell.execute_reply": "2024-11-12T01:16:51.637303Z",
     "shell.execute_reply.started": "2024-11-12T01:16:47.257407Z"
    }
   },
   "outputs": [],
   "source": [
    "df_金融性流水 = df_hyy.merge(df_xkz, how=\"left\", on=\"客户编号\")\n",
    "df_金融性流水 = df_金融性流水.merge(df_tyy, how=\"left\", on=\"客户编号\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8985b90c-6d2a-4ccd-bbe9-489f620d56a8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:16:53.737099Z",
     "iopub.status.busy": "2024-11-12T01:16:53.736590Z",
     "iopub.status.idle": "2024-11-12T01:16:53.742292Z",
     "msg_id": "89a76ea9-5352-4ae5-91af-fb1417cc5eba",
     "shell.execute_reply": "2024-11-12T01:16:53.741632Z",
     "shell.execute_reply.started": "2024-11-12T01:16:53.737067Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(59116, 2135)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_金融性流水.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f15341d-2f98-4363-94c8-bb69b621115b",
   "metadata": {},
   "source": [
    "## 税务数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "18464044-f37f-4350-9ad1-d77b24bdc6c5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:17:12.922789Z",
     "iopub.status.busy": "2024-11-12T01:17:12.922181Z",
     "iopub.status.idle": "2024-11-12T01:17:14.489903Z",
     "msg_id": "2ea4087a-3bd2-4e68-ae3b-6d2202024695",
     "shell.execute_reply": "2024-11-12T01:17:14.489099Z",
     "shell.execute_reply.started": "2024-11-12T01:17:12.922757Z"
    }
   },
   "outputs": [],
   "source": [
    "keep_feas = [\"客户编号\"] + ['是否在申报期限内申报_mean_最近24个月申报_税种10109', '一阶差分_减免税额_10109_纳税流水_月度差分_sum_-1_mean_差分特征_10109_纳税流水_月度差分', '减免税额_max_最近6个月申报_税种10101', '企业税率_min_最近12个月申报_所有税种', '减免税额_10101_纳税流水_季度差分_mean_min_差分特征_10101_纳税流水_季度差分', '全部销售收入_10104_纳税流水_季度差分_mean_min_差分特征_10104_纳税流水_季度差分', '全部销售收入_10101_纳税流水_季度差分_sum_mean_差分特征_10101_纳税流水_季度差分', '全部销售收入_10101_纳税流水_季度差分_sum_last_差分特征_10101_纳税流水_季度差分', '企业税率_min_最近6个月申报_所有税种', '企业税率_mean_最近12个月申报_所有税种', '应纳税额_sum_最近12个月申报_税种0', '企业税率_max_最近24个月申报_税种10101', '减免税额_max_最近24个月申报_税种10101', '全部销售收入_sum_最近24个月申报_税种10101', '一阶差分_应纳税额_10109_纳税流水_月度差分_sum_-1_sum_差分特征_10109_纳税流水_月度差分', '减免税额_std_最近24个月申报_税种10109', '一阶差分_应纳税额_10109_纳税流水_月度差分_sum_-1_mean_差分特征_10109_纳税流水_月度差分', '一阶差分_应税销售收入_10101_纳税流水_月度差分_mean_-1_sum_差分特征_10101_纳税流水_月度差分', '应税销售收入_全部纳税流水_月度差分_mean_last_差分特征_全部纳税流水_月度差分', '一阶差分_应税销售收入_全部纳税流水_月度差分_mean_-1_sum_差分特征_全部纳税流水_月度差分', '全部销售收入_10104_纳税流水_季度差分_mean_last_差分特征_10104_纳税流水_季度差分', '是否在申报期限内申报_mean_最近6个月申报_税种10109', '应纳税额_0_纳税流水_季度差分_sum_mean_差分特征_0_纳税流水_季度差分', '应纳税额_std_最近24个月申报_税种10109', '企业税率_mean_最近6个月申报_税种10101', '全部销售收入_10101_纳税流水_月度差分_mean_sum_差分特征_10101_纳税流水_月度差分', '一阶差分_应税销售收入_10109_纳税流水_月度差分_sum_-1_sum_差分特征_10109_纳税流水_月度差分', '全部销售收入_全部纳税流水_季度差分_mean_sum_差分特征_全部纳税流水_季度差分', '全部销售收入_max_最近24个月申报_税种10104', '应税销售收入_全部纳税流水_季度差分_mean_min_差分特征_全部纳税流水_季度差分', '一阶差分_应税销售收入_全部纳税流水_月度差分_mean_-1_mean_差分特征_全部纳税流水_月度差分', '减免税额_10101_纳税流水_月度差分_sum_mean_差分特征_10101_纳税流水_月度差分', '一阶差分_全部销售收入_10104_纳税流水_月度差分_mean_-1_last_差分特征_10104_纳税流水_月度差分', '一阶差分_应纳税额_10109_纳税流水_月度差分_mean_-1_mean_差分特征_10109_纳税流水_月度差分', '减免税额_10101_纳税流水_月度差分_sum_max_差分特征_10101_纳税流水_月度差分']\n",
    "df_税务数据 = pd.read_pickle(\"../data_bak/B_企业税务综合申报信息表_暴力衍生_1028.pkl\")[keep_feas]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "987b931c-dea0-4f2a-8be3-bd929052575a",
   "metadata": {},
   "source": [
    "## 企业变更历史"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e56797f1-fc9d-4d06-b464-fddd11685991",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:17:34.443463Z",
     "iopub.status.busy": "2024-11-12T01:17:34.442944Z",
     "iopub.status.idle": "2024-11-12T01:17:34.458700Z",
     "msg_id": "8abd5c30-9963-464a-8689-dc1d50f3de4c",
     "shell.execute_reply": "2024-11-12T01:17:34.458006Z",
     "shell.execute_reply.started": "2024-11-12T01:17:34.443432Z"
    }
   },
   "outputs": [],
   "source": [
    "df_企业变更历史 = pd.read_pickle(\"../data_bak/TYY_企业历史变更明细B榜.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c0bf2d0-8457-479c-a863-d25d10ee9ffb",
   "metadata": {},
   "source": [
    "## 企业高管"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "70aff103-39ad-412c-bcda-0cc6debb51e3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:17:52.293975Z",
     "iopub.status.busy": "2024-11-12T01:17:52.293506Z",
     "iopub.status.idle": "2024-11-12T01:17:52.318910Z",
     "msg_id": "eea9d392-39e4-449e-b067-2986544c9af0",
     "shell.execute_reply": "2024-11-12T01:17:52.318178Z",
     "shell.execute_reply.started": "2024-11-12T01:17:52.293944Z"
    }
   },
   "outputs": [],
   "source": [
    "keep_feas = [\"客户编号\"] + ['职位数量_比上_企业成立时间_年数_max_企业主要高管表', '高管数量_比上_企业成立时间_年数_max_企业主要高管表', '人员姓名_unique_counts_企业主要高管表', '高管数量_比上_企业经营时间_年数_max_企业主要高管表', '人员数量_比上_企业经营时间_年数_max_企业主要高管表', '职位数量_比上_人员总数量_max_企业主要高管表', '人员数量_比上_企业成立时间_年数_max_企业主要高管表', '职位代码_unique_counts_企业主要高管表', '人员总数量_max_企业主要高管表', '职位数量_比上_企业经营时间_年数_max_企业主要高管表', '高管数量_比上_人员总数量_max_企业主要高管表']\n",
    "df_企业高管 = pd.read_pickle(\"../data_bak/B_企业主要高管表.pkl\")[keep_feas]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d65f891-1fb7-4a4a-b27a-961ae45c88ba",
   "metadata": {},
   "source": [
    "# 股东及其出资表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4296867c-54fe-45aa-b43b-8c072d86646a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:18:08.992164Z",
     "iopub.status.busy": "2024-11-12T01:18:08.991655Z",
     "iopub.status.idle": "2024-11-12T01:18:09.008980Z",
     "msg_id": "a0ea8557-8918-48df-82cf-c723e9f9611f",
     "shell.execute_reply": "2024-11-12T01:18:09.008283Z",
     "shell.execute_reply.started": "2024-11-12T01:18:08.992132Z"
    }
   },
   "outputs": [],
   "source": [
    "df_tyy_股东及其出资 = pd.read_pickle(\"../data_bak/TYY_企业股东及出资表B榜.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ceedf92b-cdbf-46e9-ae35-00f946e0a436",
   "metadata": {},
   "source": [
    "# 企业终本案件明细/被执行人人明细/失信被执行人明细"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ab08b925-000a-4d27-8ddd-54f8adb4d54e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:18:13.119141Z",
     "iopub.status.busy": "2024-11-12T01:18:13.118666Z",
     "iopub.status.idle": "2024-11-12T01:18:13.155921Z",
     "msg_id": "be7dec1a-3ac8-4a82-bfa5-ad13660782c8",
     "shell.execute_reply": "2024-11-12T01:18:13.155228Z",
     "shell.execute_reply.started": "2024-11-12T01:18:13.119110Z"
    }
   },
   "outputs": [],
   "source": [
    "tmp = get_data(\"XW_ENTINFO_PUNISHED\")\n",
    "\n",
    "df_被执行人 = tmp[[\"客户编号\"]].drop_duplicates()\n",
    "df_被执行人[\"是否被执行人\"] = 1\n",
    "\n",
    "tmp = get_data(\"XW_ENTINFO_PUNISHBREAK\")\n",
    "\n",
    "df_失信 = tmp[[\"客户编号\"]].drop_duplicates()\n",
    "df_失信[\"是否失信\"] = 1\n",
    "\n",
    "tmp = get_data(\"XW_ENTINFO_FINALCASE\")\n",
    "\n",
    "df_终本 = tmp[[\"客户编号\"]].drop_duplicates()\n",
    "df_终本[\"是否终本案件\"] = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca0eeb09-0281-4f6a-8a2d-4dc7fe26cfa5",
   "metadata": {},
   "source": [
    "## 新做税收特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3840f75a-efd4-41b7-9818-2dc219262a8f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:18:31.978896Z",
     "iopub.status.busy": "2024-11-12T01:18:31.978419Z",
     "iopub.status.idle": "2024-11-12T01:18:31.987980Z",
     "msg_id": "1ac8bedc-524a-48a5-b177-a39214b9e9f7",
     "shell.execute_reply": "2024-11-12T01:18:31.987284Z",
     "shell.execute_reply.started": "2024-11-12T01:18:31.978865Z"
    }
   },
   "outputs": [],
   "source": [
    "df_tyy_税收 = pd.read_pickle(\"../data_bak/TYY_企业税务暴力特征B榜.pkl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4270400d-459e-4fe6-958f-7c7b1dd00a11",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:18:56.309777Z",
     "iopub.status.busy": "2024-11-12T01:18:56.309305Z",
     "iopub.status.idle": "2024-11-12T01:18:56.325998Z",
     "msg_id": "a35a88ab-e2f7-4da8-88f3-6055180c2287",
     "shell.execute_reply": "2024-11-12T01:18:56.325213Z",
     "shell.execute_reply.started": "2024-11-12T01:18:56.309746Z"
    }
   },
   "outputs": [],
   "source": [
    "df_hyy_企业基本信息 = pd.read_pickle(\"../data_bak/基本信息表补充两个特征_B榜.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70495eac-0792-469c-a3f7-f406dac5e31d",
   "metadata": {},
   "source": [
    "## 合并数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "e69113f4-0183-454b-9890-809b8cbd1ce9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T06:34:48.347688Z",
     "iopub.status.busy": "2024-11-12T06:34:48.347197Z",
     "iopub.status.idle": "2024-11-12T06:35:16.325618Z",
     "msg_id": "5a31685d-fddd-4113-acd2-c1c6dc62b367",
     "shell.execute_reply": "2024-11-12T06:35:16.324805Z",
     "shell.execute_reply.started": "2024-11-12T06:34:48.347656Z"
    }
   },
   "outputs": [],
   "source": [
    "df = TARGET[[\"客户编号\",\"FLAG\"]].merge(df_金融性流水, how=\"left\", on=\"客户编号\")\n",
    "df = df.merge(df_企业基本信息, how=\"left\", on=\"客户编号\")\n",
    "df = df.merge(df_税务数据, how=\"left\", on=\"客户编号\")\n",
    "\n",
    "df = df.merge(df_企业变更历史, how=\"left\", on=\"客户编号\")\n",
    "df = df.merge(df_企业高管, how=\"left\", on=\"客户编号\")\n",
    "df = df.merge(df_tyy_股东及其出资, how=\"left\", on=\"客户编号\")\n",
    "\n",
    "df = df.merge(df_被执行人, how=\"left\", on=\"客户编号\")\n",
    "df = df.merge(df_失信, how=\"left\", on=\"客户编号\")\n",
    "df = df.merge(df_终本, how=\"left\", on=\"客户编号\")\n",
    "\n",
    "df = df.merge(df_tyy_税收, how=\"left\", on=\"客户编号\")\n",
    "df = df.merge(df_hyy_企业基本信息, how=\"left\", on=\"客户编号\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "e4ddb445-fea6-4ca1-b0b9-532b8be8fe60",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T06:35:16.327338Z",
     "iopub.status.busy": "2024-11-12T06:35:16.326990Z",
     "iopub.status.idle": "2024-11-12T06:35:16.403264Z",
     "msg_id": "7d48152f-8a24-4a33-8597-c14e93ee134e",
     "shell.execute_reply": "2024-11-12T06:35:16.402627Z",
     "shell.execute_reply.started": "2024-11-12T06:35:16.327310Z"
    }
   },
   "outputs": [],
   "source": [
    "df[\"是否被执行人\"] = df[\"是否被执行人\"].apply(lambda x: 1 if x==1 else 0)\n",
    "df[\"是否失信\"] = df[\"是否失信\"].apply(lambda x: 1 if x==1 else 0)\n",
    "df[\"是否终本案件\"] = df[\"是否终本案件\"].apply(lambda x: 1 if x==1 else 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "d2feb485-9689-4009-8757-3e405da00693",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:19:44.019596Z",
     "iopub.status.busy": "2024-11-12T01:19:44.019118Z",
     "iopub.status.idle": "2024-11-12T01:19:44.024748Z",
     "msg_id": "1c9370dc-bc16-4dde-bd42-5479f2d5241f",
     "shell.execute_reply": "2024-11-12T01:19:44.024091Z",
     "shell.execute_reply.started": "2024-11-12T01:19:44.019566Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(59116, 2414)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87d03ba5-0190-4332-8cf0-18af0ee8ce53",
   "metadata": {},
   "source": [
    "## XGB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "1594929d-aad0-4a3e-ae2f-3dd6bcf4a4e1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T01:20:52.324212Z",
     "iopub.status.busy": "2024-11-12T01:20:52.323720Z",
     "iopub.status.idle": "2024-11-12T01:20:57.292029Z",
     "msg_id": "3d050c42-c274-4801-bf9f-a8fc28f5c0fa",
     "shell.execute_reply": "2024-11-12T01:20:57.291223Z",
     "shell.execute_reply.started": "2024-11-12T01:20:52.324180Z"
    }
   },
   "outputs": [],
   "source": [
    "X = df[df[\"FLAG\"].notnull()].drop(columns=[\"客户编号\",\"FLAG\"])\n",
    "y = df[df[\"FLAG\"].notnull()][\"FLAG\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "0b3314b6-9cab-4162-ace8-7bb4dc9f64fd",
   "metadata": {
    "collapsed": true,
    "execution": {
     "iopub.execute_input": "2024-11-12T01:21:02.918636Z",
     "iopub.status.busy": "2024-11-12T01:21:02.918153Z",
     "iopub.status.idle": "2024-11-12T02:11:39.909990Z",
     "msg_id": "508836ce-2a03-433b-b1bb-ee9cb5f552ae",
     "shell.execute_reply": "2024-11-12T02:11:39.909225Z",
     "shell.execute_reply.started": "2024-11-12T01:21:02.918606Z"
    },
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0]\ttrain-auc:0.73511\tvalid-auc:0.71765\n",
      "[100]\ttrain-auc:0.86570\tvalid-auc:0.82074\n",
      "[200]\ttrain-auc:0.89621\tvalid-auc:0.83726\n",
      "[300]\ttrain-auc:0.91757\tvalid-auc:0.84277\n",
      "[400]\ttrain-auc:0.93344\tvalid-auc:0.84572\n",
      "[500]\ttrain-auc:0.94562\tvalid-auc:0.84789\n",
      "[600]\ttrain-auc:0.95522\tvalid-auc:0.84801\n",
      "[700]\ttrain-auc:0.96337\tvalid-auc:0.84822\n",
      "[800]\ttrain-auc:0.97013\tvalid-auc:0.84818\n",
      "[900]\ttrain-auc:0.97571\tvalid-auc:0.84824\n",
      "[1000]\ttrain-auc:0.98023\tvalid-auc:0.84926\n",
      "[1100]\ttrain-auc:0.98418\tvalid-auc:0.84970\n",
      "[1200]\ttrain-auc:0.98710\tvalid-auc:0.85000\n",
      "[1300]\ttrain-auc:0.98959\tvalid-auc:0.84982\n",
      "[1326]\ttrain-auc:0.99008\tvalid-auc:0.84961\n",
      "=======================================\n",
      "第 1 折，当前 KS = 0.536143\n",
      "=======================================\n",
      "[0]\ttrain-auc:0.73824\tvalid-auc:0.71075\n",
      "[100]\ttrain-auc:0.86715\tvalid-auc:0.82070\n",
      "[200]\ttrain-auc:0.89785\tvalid-auc:0.83578\n",
      "[300]\ttrain-auc:0.91833\tvalid-auc:0.84095\n",
      "[400]\ttrain-auc:0.93364\tvalid-auc:0.84419\n",
      "[500]\ttrain-auc:0.94638\tvalid-auc:0.84523\n",
      "[600]\ttrain-auc:0.95618\tvalid-auc:0.84499\n",
      "[700]\ttrain-auc:0.96409\tvalid-auc:0.84645\n",
      "[800]\ttrain-auc:0.97032\tvalid-auc:0.84632\n",
      "[900]\ttrain-auc:0.97566\tvalid-auc:0.84659\n",
      "[1000]\ttrain-auc:0.98042\tvalid-auc:0.84701\n",
      "[1100]\ttrain-auc:0.98405\tvalid-auc:0.84758\n",
      "[1200]\ttrain-auc:0.98688\tvalid-auc:0.84755\n",
      "[1300]\ttrain-auc:0.98930\tvalid-auc:0.84806\n",
      "[1400]\ttrain-auc:0.99136\tvalid-auc:0.84798\n",
      "[1500]\ttrain-auc:0.99311\tvalid-auc:0.84823\n",
      "[1600]\ttrain-auc:0.99436\tvalid-auc:0.84809\n",
      "[1700]\ttrain-auc:0.99555\tvalid-auc:0.84790\n",
      "[1734]\ttrain-auc:0.99581\tvalid-auc:0.84779\n",
      "=======================================\n",
      "第 2 折，当前 KS = 0.55063\n",
      "=======================================\n",
      "[0]\ttrain-auc:0.72816\tvalid-auc:0.74124\n",
      "[100]\ttrain-auc:0.86406\tvalid-auc:0.84238\n",
      "[200]\ttrain-auc:0.89562\tvalid-auc:0.85359\n",
      "[300]\ttrain-auc:0.91636\tvalid-auc:0.85638\n",
      "[400]\ttrain-auc:0.93265\tvalid-auc:0.85669\n",
      "[500]\ttrain-auc:0.94523\tvalid-auc:0.85664\n",
      "[600]\ttrain-auc:0.95533\tvalid-auc:0.85788\n",
      "[700]\ttrain-auc:0.96359\tvalid-auc:0.85701\n",
      "[788]\ttrain-auc:0.96978\tvalid-auc:0.85666\n",
      "=======================================\n",
      "第 3 折，当前 KS = 0.565813\n",
      "=======================================\n",
      "[0]\ttrain-auc:0.73216\tvalid-auc:0.72900\n",
      "[100]\ttrain-auc:0.86677\tvalid-auc:0.83468\n",
      "[200]\ttrain-auc:0.89615\tvalid-auc:0.84729\n",
      "[300]\ttrain-auc:0.91713\tvalid-auc:0.85322\n",
      "[400]\ttrain-auc:0.93294\tvalid-auc:0.85657\n",
      "[500]\ttrain-auc:0.94607\tvalid-auc:0.85851\n",
      "[600]\ttrain-auc:0.95635\tvalid-auc:0.85977\n",
      "[700]\ttrain-auc:0.96459\tvalid-auc:0.86111\n",
      "[800]\ttrain-auc:0.97108\tvalid-auc:0.86162\n",
      "[900]\ttrain-auc:0.97624\tvalid-auc:0.86062\n",
      "[997]\ttrain-auc:0.98080\tvalid-auc:0.86063\n",
      "=======================================\n",
      "第 4 折，当前 KS = 0.561624\n",
      "=======================================\n",
      "[0]\ttrain-auc:0.72944\tvalid-auc:0.71764\n",
      "[100]\ttrain-auc:0.86653\tvalid-auc:0.82995\n",
      "[200]\ttrain-auc:0.89719\tvalid-auc:0.84371\n",
      "[300]\ttrain-auc:0.91833\tvalid-auc:0.84868\n",
      "[400]\ttrain-auc:0.93450\tvalid-auc:0.85060\n",
      "[500]\ttrain-auc:0.94683\tvalid-auc:0.85305\n",
      "[600]\ttrain-auc:0.95753\tvalid-auc:0.85435\n",
      "[700]\ttrain-auc:0.96553\tvalid-auc:0.85439\n",
      "[800]\ttrain-auc:0.97201\tvalid-auc:0.85460\n",
      "[900]\ttrain-auc:0.97722\tvalid-auc:0.85360\n",
      "[962]\ttrain-auc:0.97997\tvalid-auc:0.85280\n",
      "=======================================\n",
      "第 5 折，当前 KS = 0.549218\n",
      "=======================================\n",
      "平均 KS = 0.552686\n",
      "Out of folds KS = 0.545255\n"
     ]
    }
   ],
   "source": [
    "X = X.replace(np.inf,np.nan)\n",
    "X = X.replace(-np.inf,np.nan)\n",
    "\n",
    "params={\n",
    "        'booster':'gbtree',\n",
    "        'objective':'binary:logistic',\n",
    "        'eval_metric':'auc',\n",
    "        'gamma':0.1,\n",
    "        'min_child_weight':1.1,\n",
    "        'max_depth':3,\n",
    "        'lambda':10,\n",
    "        'alpha':1,\n",
    "        'subsample':0.7,\n",
    "        'colsample_bytree':0.7,\n",
    "        'colsample_bylevel':0.7,\n",
    "        'eta':0.05,\n",
    "        'tree_method':'exact',\n",
    "        'seed':1000,\n",
    "        'nthread':12   \n",
    "    }\n",
    "clfs_xgb = XGB_model(X, y, params=params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "d6256e2f-50fb-4dd1-9302-7ce5e649a522",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T02:12:09.650891Z",
     "iopub.status.busy": "2024-11-12T02:12:09.650408Z",
     "iopub.status.idle": "2024-11-12T02:12:09.774726Z",
     "msg_id": "7df3728d-b259-4f49-95b2-8ac5b16d9111",
     "shell.execute_reply": "2024-11-12T02:12:09.773954Z",
     "shell.execute_reply.started": "2024-11-12T02:12:09.650858Z"
    }
   },
   "outputs": [],
   "source": [
    "with open(\"./模型文件/B榜主模型_XGB.pkl\", \"wb\") as f:\n",
    "    pickle.dump(clfs_xgb, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "ae30d4ee-680f-4af6-804d-9c8e1717851a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T06:44:48.228569Z",
     "iopub.status.busy": "2024-11-12T06:44:48.227657Z",
     "iopub.status.idle": "2024-11-12T06:44:50.935651Z",
     "msg_id": "3f078362-a24f-4b40-8a68-06ebb337e922",
     "shell.execute_reply": "2024-11-12T06:44:50.934793Z",
     "shell.execute_reply.started": "2024-11-12T06:44:48.228537Z"
    }
   },
   "outputs": [],
   "source": [
    "# xgb预测\n",
    "X_test = df[df[\"FLAG\"].isnull()][X.columns.tolist()]\n",
    "X_test.replace(np.inf,np.nan,inplace=True)\n",
    "X_test.replace(-np.inf,np.nan,inplace=True)\n",
    "\n",
    "i = 1\n",
    "submit = pd.DataFrame()\n",
    "submit[\"客户编号\"] = df[df[\"FLAG\"].isnull()][\"客户编号\"]\n",
    "for model in clfs_xgb[0]:\n",
    "    X_test_DM = xgb.DMatrix(data=X_test, label=None)\n",
    "    tmp_pred = model.predict(X_test_DM)\n",
    "    submit[\"XGB_score_{}\".format(i)] = tmp_pred\n",
    "    i = i + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "d93604e7-7a8e-4857-9f92-dc452ec3356d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T06:44:52.298124Z",
     "iopub.status.busy": "2024-11-12T06:44:52.297236Z",
     "iopub.status.idle": "2024-11-12T06:44:52.304659Z",
     "msg_id": "8d180bd7-422c-44f7-8c73-16159a33a34f",
     "shell.execute_reply": "2024-11-12T06:44:52.303949Z",
     "shell.execute_reply.started": "2024-11-12T06:44:52.298094Z"
    }
   },
   "outputs": [],
   "source": [
    "submit[\"平均分数\"] = submit[[\"XGB_score_1\",\"XGB_score_2\",\"XGB_score_3\",\"XGB_score_4\",\"XGB_score_5\"]].mean(axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba3761f3-bac8-468c-8c9c-44cc1f535a75",
   "metadata": {},
   "source": [
    "## 融合专家规则模型结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "d4fed4e0-c2a7-4f28-a45a-e4c718e578f5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T06:44:56.582423Z",
     "iopub.status.busy": "2024-11-12T06:44:56.582048Z",
     "iopub.status.idle": "2024-11-12T06:44:59.371497Z",
     "msg_id": "6649f09a-77dc-45c7-8ed0-e63705022e37",
     "shell.execute_reply": "2024-11-12T06:44:59.370681Z",
     "shell.execute_reply.started": "2024-11-12T06:44:56.582393Z"
    }
   },
   "outputs": [],
   "source": [
    "df_new = pd.read_pickle(\"../data_bak/B_金融性交易明细_平均特征.pkl\")\n",
    "submit = submit.merge(df, how=\"left\", on=\"客户编号\")\n",
    "submit = submit.merge(df_new,how=\"left\", on=\"客户编号\")\n",
    "submit[\"平均分数\"][(submit[\"省份交易金额_总额\"] > 651540.4688) & (submit[\"行业平均合约账户余额\"] > 33.8613) & (submit[\"省份交易金额_平均额\"] <= 25.4383) & (submit[\"交易金额总额_比上_类型总额\"] > 3.4011) & (submit[\"折人民币交易金额_总额\"] <= 5242.0249) |(submit[\"省份交易金额_总额\"] <= 651540.4688) & (submit[\"行业交易金额_平均额\"] <= 24.513) & (submit[\"企业交易金额_总额\"] <= 89157976.0) |\n",
    "(submit[\"行业平均合约账户余额\"] <= 33.8613) & (submit[\"折人民币交易金额_总额\"] <= 9.385) & (submit[\"交易金额平均额_比上_类型平均\"] > 0.0479) |\n",
    "(submit[\"行业平均合约账户余额\"] <= 33.8613) & (submit[\"折人民币交易金额_总额\"] > 9.385) & (submit[\"企业交易金额_平均额\"] > 33.6355) & (submit[\"XGB_score_2\"] > 0.026) & (submit[\"XGB_score_1\"] <= 0.0207) |\n",
    "(submit[\"行业平均合约账户余额\"] > 33.8613) & (submit[\"交易金额平均额_比上_省份平均\"] <= 0.3229) & (submit[\"交易金额平均额_比上_省份平均\"] <= 0.3229) & (submit[\"行业交易金额_平均额\"] > 35.9201) & (submit[\"交易金额平均额_比上_行业平均\"] > 0.2627) |\n",
    "(submit[\"行业平均合约账户余额\"] > 33.8613) & (submit[\"交易金额平均额_比上_省份平均\"] > 0.3229) & (submit[\"省份交易金额_总额\"] <= 1792757.4375) & (submit[\"交易金额平均额_比上_省份平均\"] <= 0.512) & (submit[\"交易金额平均额_比上_类型平均\"] > 0.5174)|\n",
    "(submit[\"一阶差分_折人民币交易金额_金融性交易_跨行1.0_按7天流水_sum_-1_get_kurt_差分特征_金融性交易_跨行1.0_按7天流水\"] > -4.40863299369812) & (submit[\"合约账户余额_金融性交易_记账方向代码125_15天流水_mean_get_kurt_差分特征_金融性交易_记账方向代码125_15天流水\"] > -5.760505676269531) & (submit[\"合约账户余额_金融性交易_同名0.0_按7天流水_count_get_kurt_差分特征_金融性交易_同名0.0_按7天流水\"] <= -3.316666603088379) & (submit[\"一阶差分_合约账户余额_金融性交易_记账方向代码164_7天流水_mean_-1_min_差分特征_金融性交易_记账方向代码164_7天流水\"] > -5.638000011444092)] = submit[\"平均分数\"].max()\n",
    "\n",
    "submit[[\"客户编号\",\"平均分数\"]].to_csv(\"./B榜结果.csv\", index=False, header=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6664beec-61aa-42d9-a7d7-14927011055b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "9c186de8-ec7c-453e-9910-be1ca7a9a1a9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-28T11:49:14.951444Z",
     "iopub.status.busy": "2024-10-28T11:49:14.950960Z",
     "iopub.status.idle": "2024-10-28T11:49:14.954797Z",
     "msg_id": "beb4bf2c-c049-4fd5-9e64-fdd91689800e",
     "shell.execute_reply": "2024-10-28T11:49:14.954147Z",
     "shell.execute_reply.started": "2024-10-28T11:49:14.951412Z"
    }
   },
   "source": [
    "# 提交"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "9a09c876-a593-44a6-91c5-0efd8d545018",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T06:38:03.934187Z",
     "iopub.status.busy": "2024-11-12T06:38:03.933687Z",
     "iopub.status.idle": "2024-11-12T06:38:03.941212Z",
     "msg_id": "d6655e5e-4605-4904-a21e-fa10df8a6e98",
     "shell.execute_reply": "2024-11-12T06:38:03.940355Z",
     "shell.execute_reply.started": "2024-11-12T06:38:03.934156Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The prv extension is already loaded. To reload it, use:\n",
      "  %reload_ext prv\n",
      "Predict init complete.\n",
      "Matplotlib env init complete.\n",
      "Gbase数据库信息配置为空，相关魔法命令不可使用，如有需求，请联系管理员配置\n"
     ]
    }
   ],
   "source": [
    "init_woody"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "641c7b42-8a72-4f01-b0f5-295d7f37ca1e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T06:45:05.984647Z",
     "iopub.status.busy": "2024-11-12T06:45:05.984214Z",
     "iopub.status.idle": "2024-11-12T06:45:06.291793Z",
     "msg_id": "0aec4d4b-5b6f-4535-99be-3a7f6b40d91a",
     "shell.execute_reply": "2024-11-12T06:45:06.291092Z",
     "shell.execute_reply.started": "2024-11-12T06:45:05.984617Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "评分提交成功！\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'请稍后使用命令: %query_predict problem_id 查看评分结果, problem_id为阶段序号，取值为：1,2, 比如查询第一阶段的评分结果: %query_predict 1'"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%predict 4 B榜结果.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "84f12e66-4e01-4383-8649-8795d503cbd1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-12T06:45:12.351006Z",
     "iopub.status.busy": "2024-11-12T06:45:12.350522Z",
     "iopub.status.idle": "2024-11-12T06:45:12.594039Z",
     "msg_id": "b12f2cf2-3e54-4db8-a73b-5fd4ff9ad7f3",
     "shell.execute_reply": "2024-11-12T06:45:12.593334Z",
     "shell.execute_reply.started": "2024-11-12T06:45:12.350975Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最近三次评分提交的结果(供参考):\n",
      "\n",
      "提交时间：2024-11-12 14:45:06 \t 评分结果：0.58044    \t 评分成功\n",
      "提交时间：2024-11-12 14:45:04 \t 评分结果：0.58044    \t 评分成功\n",
      "提交时间：2024-11-12 14:42:38 \t 评分结果：0.58044    \t 评分成功\n"
     ]
    }
   ],
   "source": [
    "%query_predict 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5bfeaf9-6ec7-4bf0-a368-d1a7f4c9f378",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
